package com.atguigu.edu.contorller;

import com.atguigu.edu.bean.PaperClassStats;
import com.atguigu.edu.bean.PaperExamStats;
import com.atguigu.edu.service.PaperClassStatsService;
import com.atguigu.edu.util.DateFormatUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PaperClassStatsController {
    @Autowired
    private PaperClassStatsService paperClassStatsService;

    @RequestMapping("/paperclass")
    public String edu(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<PaperClassStats> paperClassStats = paperClassStatsService.selectPaperClass(date);

        StringBuilder jsonClass = new StringBuilder("{\n" +
                "  \"status\": 0,\n" +
                "  \"data\": {");
        jsonClass.append("\"total\": " + paperClassStats.size() + ",");
        jsonClass.append("   \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"课程名称\",\n" +
                "        \"id\": \"course_name\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"试卷考试人数\",\n" +
                "        \"id\": \"testExamPeople\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"试卷平均分\",\n" +
                "        \"id\": \"testAvgScore\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"试卷平均时长\",\n" +
                "        \"id\": \"testAvgTime\"\n" +
                "      }\n" +
                "    ], \"superHeaders\": \n" +
                "      [\n" +
                "        {\n" +
                "          \"name\": \"粒度\",\n" +
                "          \"colspan\": 2\n" +
                "        },\n" +
                "        {\n" +
                "          \"name\": \"统计\",\n" +
                "          \"colspan\": 2\n" +
                "        }\n" +
                "      ]\n" +
                "    ,\"rows\": [");

        for (int i = 0; i < paperClassStats.size(); i++) {
            PaperClassStats classStats = paperClassStats.get(i);


            jsonClass.append("      {\n" +
                    "        \"course_name\": \"" +  classStats.getCourse_name() + "\",\n" +
                    "        \"testExamPeople\": \"" + classStats.getTestExamPeople() + "\",\n" +
                    "        \"testAvgScore\": \"" + classStats.getTestAvgScore() + "\",\n" +
                    "        \"testAvgTime\": " + classStats.getTestAvgTime() + "\n" +
                    "      }");


            if ( i < paperClassStats.size() - 1) {
                jsonClass.append(",");
            }
        }


        jsonClass.append("]}}");
        String replace = null;
      if (jsonClass.toString().contains("\\")){
          replace = jsonClass.toString().replace("\\", "_");
          System.out.println("000000000000000000");
          return replace;
        }else {
          return jsonClass.toString();
      }
        //System.out.println(jsonClass.toString());
    }
}
